package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.domain.vo.SoulQuestionList2Vo;
import com.itheima.domain.vo.SoulQuestionVo;
import com.tanhua.api.db.SoulQuestionApi;
import com.tanhua.api.db.SoulQuestionListApi;
import com.tanhua.api.db.SoulQuestionOptionApi;
import com.tanhua.domain.db.SoulQuestion;
import com.tanhua.domain.db.SoulQuestionList;
import com.tanhua.domain.db.SoulQuestionOption;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fone
 * @date 2021/09/05
 */

@Service
public class SoulService {


    @Reference
    private SoulQuestionApi soulQuestionApi;


    @Reference
    private SoulQuestionListApi soulQuestionListApi;


    @Reference
    private SoulQuestionOptionApi soulQuestionOptionApi;

    /**
     * 分页查询测灵魂问题
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    public ResponseEntity findByPage(Integer pageNum, Integer pageSize, Integer levelId, String keyword) {

        IPage iPage = soulQuestionApi.findByPage(pageNum, pageSize, levelId, keyword);

        PageBeanVo pageBeanVo = new PageBeanVo(pageNum, pageSize, iPage.getTotal(), iPage.getRecords());

        List<SoulQuestion> soulQuestionList = (List<SoulQuestion>) pageBeanVo.getItems();


        List<SoulQuestionList2Vo> soulQuestionVoList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(soulQuestionList)) {

            for (SoulQuestion soulQuestion : soulQuestionList) {
                SoulQuestionList2Vo vo = new SoulQuestionList2Vo();

                vo.setId(soulQuestion.getId().longValue());
                vo.setQuestion(soulQuestion.getQuestion());
                vo.setCreated(soulQuestion.getCreated());
                vo.setUpdated(soulQuestion.getUpdated());


                // 查询测灵魂问题到等级信息
                SoulQuestionList soulQuestionLevel = soulQuestionListApi.findById(soulQuestion.getQuestionLevel());

                vo.setLevelId(Long.valueOf(soulQuestionLevel.getId()));
                vo.setName(soulQuestionLevel.getName());
                vo.setLevel(soulQuestionLevel.getLevel());
                vo.setStar(soulQuestionLevel.getStar());

                soulQuestionVoList.add(vo);
            }
        }

        pageBeanVo.setItems(soulQuestionVoList);

        return ResponseEntity.ok(pageBeanVo);
    }


    /**
     * 查询问题等级
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ResponseEntity findQuestionLevelByPage(Integer pageNum, Integer pageSize) {

        // 查询所有问题等级
        List<SoulQuestionList> soulQuestionLevelList = soulQuestionListApi.findAll();

        return ResponseEntity.ok(soulQuestionLevelList);
    }

    /**
     * 新增测灵魂问题
     * @param soulQuestionVo
     * @return
     */
    public ResponseEntity addQuestion(SoulQuestionVo soulQuestionVo) {

        SoulQuestion soulQuestion = new SoulQuestion();
        soulQuestion.setQuestion(soulQuestionVo.getQuestion());
        soulQuestion.setQuestionLevel(soulQuestionVo.getQuestionLevel().intValue());

        // 向数据库添加问题， 返回 question 的 id
        Integer id = soulQuestionApi.save(soulQuestion);

        List<SoulQuestionOption> optionList = soulQuestionVo.getOptions();

        for (SoulQuestionOption soulQuestionOption : optionList) {

            // 给选项设置 question id
            soulQuestionOption.setQuestionId(id);

            // 保存选项
            soulQuestionOptionApi.save(soulQuestionOption);
        }

        return ResponseEntity.ok("success");

    }

    /**
     * 根据 question id 查询指定 question
     * @param id
     * @return
     */
    public ResponseEntity findQuestionById(Integer id) {

        SoulQuestionVo vo = new SoulQuestionVo();

        SoulQuestion soulQuestion = soulQuestionApi.findById(id);

        vo.setId(Long.valueOf(soulQuestion.getId()));
        vo.setQuestion(soulQuestion.getQuestion());
        vo.setQuestionLevel(Long.valueOf(soulQuestion.getQuestionLevel()));

        // 查询指定问题的所有选项
        List<SoulQuestionOption> soulQuestionOptionList = soulQuestionOptionApi.findByQuestionId(soulQuestion.getId());

        vo.setOptions(soulQuestionOptionList);

        return ResponseEntity.ok(vo);
    }

    /**
     * 修改问题
     * @param soulQuestionVo
     * @return
     */
    public ResponseEntity updateQuestion(SoulQuestionVo soulQuestionVo) {

        SoulQuestion soulQuestion = new SoulQuestion();
        soulQuestion.setId(soulQuestionVo.getId().intValue());
        soulQuestion.setQuestion(soulQuestionVo.getQuestion());
        soulQuestion.setQuestionLevel(soulQuestionVo.getQuestionLevel().intValue());

        // 更新问题
        soulQuestionApi.update(soulQuestion);

        // 先删除选项
        soulQuestionOptionApi.deleteByQuestionId(soulQuestion.getId());

        // 重新添加选项
        List<SoulQuestionOption> optionList = soulQuestionVo.getOptions();

        if (optionList != null) {

            for (SoulQuestionOption soulQuestionOption : optionList) {

                if (!"".equals(soulQuestionOption.getScore())) {
                    // 给选项设置 question id
                    soulQuestionOption.setQuestionId(soulQuestion.getId());

                    // 保存选项
                    soulQuestionOptionApi.save(soulQuestionOption);
                }
            }
        }

        return ResponseEntity.ok("success");
    }

    /**
     * 根据问题id查询选项
     * @param questionId
     * @return
     */
    public ResponseEntity findOptionByQuestionId(Integer questionId) {
        List<SoulQuestionOption> questionOptionList = soulQuestionOptionApi.findByQuestionId(questionId);

        return ResponseEntity.ok(questionOptionList);
    }

    /**
     * 根据 id 删除问题
     * @param id
     * @return
     */
    public ResponseEntity deleteQuestion(Integer id) {

        // 因为有外键，先删除选项再删除 问题
        soulQuestionOptionApi.deleteByQuestionId(id);

        // 删除问题
        soulQuestionApi.deleteById(id);


        return ResponseEntity.ok("success");
    }
}
